package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.RecyclerView;

public class RecycleViewActivity extends AppCompatActivity {
    private String[] names = {"张三", "李四", "王五", "赵六", "孙七", "周八", "吴九"};
    private String[] ages = {"18", "19", "20", "21", "22", "23", "24"};
    private RecyclerView recycleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_recycle_view);
        recycleView = findViewById(R.id.recycle_view);
        recycleView.setAdapter(new MyAdapter());
        onItemClickListener = new onItemClickListener() {
            @Override
            public void onclick(int position) {
                Toast.makeText(RecycleViewActivity.this, "姓名："+names[position] +","+"年龄："+ages[position], Toast.LENGTH_SHORT).show();
            }
        };
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    private class MyAdapter extends RecyclerView.Adapter <MyAdapter.MyViewHolder>{
        @NonNull
        @Override
        //创建ViewHolder
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(RecycleViewActivity.this, R.layout.item_list, null);
            return new MyViewHolder(view);
        }
        //绑定数据
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.imageView.setImageResource(R.drawable.ic_launcher_foreground);
            holder.name.setText(names[position]);
            holder.age.setText(ages[position]);
            //添加点击事件
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener!=null){
                        int adapterPosition = holder.getAdapterPosition();
                        if (adapterPosition != RecyclerView.NO_POSITION) {
                            onItemClickListener.onclick(adapterPosition);
                        }
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return names.length;
        }
        //数据类型
        class MyViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            TextView name,age;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.imageView);
                name = itemView.findViewById(R.id.name);
                age = itemView.findViewById(R.id.age);
            }
        }
    }
    private onItemClickListener onItemClickListener;
    interface onItemClickListener{
        //点击事件，传入一个位置
        void onclick(int position);
    }
}